/*
 * @Description: 原文挂接
 * @Author: Rfan
 * @Date: 2022-05-04 19:07:54
 * @LastEditTime: 2022-05-23 15:25:05
 */

import { useRef, useState } from 'react';
import { Button, message, Modal, Space } from 'antd';
import classNames from 'classnames/bind';
import RulePane from './components/RulePane';
import { useBatchManageContext } from '../../../contexts/BatchManageContext';
import TransferTemplate from '@/components/TransferTemplate';
import { EnumTemplateTypes } from '@/view/system/TemplateManage/dictionaries';
import { InputStatus } from 'antd/es/_util/statusUtils';
import type { ICommonDialog } from '@/interfaces/global';
import styles from '../../../styles/uploadFolderDialog.module.scss';
import { ReceiveDialogContext } from '@/view/accession/BatchManage/components/dialogs/Receive/contexts/ReceiveDialogContext';

const cx = classNames.bind(styles);

interface UploadOriginDialogProps extends ICommonDialog {}

const ReceiveDialog = (props: UploadOriginDialogProps) => {
  const { visible, onClose } = props;
  const { activeBatch } = useBatchManageContext();
  const ruleRef = useRef(null);

  // 选中的模板
  const [selectTemplate, setSelectTemplate] = useState(null);
  // 模板选择框状态
  const [templateState, setTemplateState] = useState<InputStatus>('');

  const onCancel = () => {
    onClose();
  };

  const submitDialog = async () => {
    if (selectTemplate) {
      ruleRef.current.save();
    } else {
      setTemplateState('error');
      message.error('请选择模板');
      return false;
    }
  };

  return (
    <Modal
      title="接收"
      visible={visible}
      className="upload-origin-dialog"
      width={600}
      maskClosable={false}
      destroyOnClose
      onCancel={onCancel}
      footer={[
        <Button key="close" onClick={onCancel}>
          关闭
        </Button>,
        <Button
          key="save"
          onClick={() => {
            submitDialog();
          }}
        >
          保存
        </Button>
      ]}
    >
      <ReceiveDialogContext.Provider value={{ templateId: selectTemplate, onClose }}>
        <p>
          当前批次档案为：全宗{activeBatch.fonds?.fondName} - 档案类型
          {activeBatch.archiveType?.atName}
        </p>
        <Space className={cx('select-template__wrap')}>
          <h4>请选择模板</h4>
          <TransferTemplate
            tplType={EnumTemplateTypes.ExcelImport}
            value={selectTemplate}
            templateState={templateState}
            onChange={setSelectTemplate}
          />
        </Space>
        <RulePane
          ref={ruleRef}
          batchId={activeBatch.id}
          atId={activeBatch.archiveTypeId}
          rule={activeBatch.attOriginalRule}
        />
      </ReceiveDialogContext.Provider>
    </Modal>
  );
};

export default ReceiveDialog;
